package com.yiqi.liebang.feature.mine.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.CouponBo;
import com.yiqi.liebang.feature.mine.contract.CouponContract;
import com.yiqi.liebang.feature.mine.dependencies.coupon.CouponPresenterModule;
import com.yiqi.liebang.feature.mine.dependencies.coupon.DaggerCouponComponent;
import com.yiqi.liebang.feature.mine.view.adapter.CouponAdpter;
import com.yiqi.liebang.feature.mine.view.adapter.SelectCouponAdpter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectCouponActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener, OnRefreshListener,
        CouponContract.View, BaseQuickAdapter.RequestLoadMoreListener {


    @Inject
    CouponContract.Presenter mPresenter;

    @BindView(R.id.rv_coupon)
    RecyclerView mRvCoupon;

    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    private int mType = 0;
    private SelectCouponAdpter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<CouponBo> mData;
    private int mCurrentCounter = 0;

    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private String mLabelsStr;
    private boolean isShow;
    private double mAskPrize;


    @Override
    public int attachLayoutRes() {
        return R.layout.activity_select_coupon;
    }

    @Override
    protected void initInjector() {
        DaggerCouponComponent.builder().couponPresenterModule(new CouponPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        mAskPrize = getIntent().getDoubleExtra("ask_prize", 0.0);
        initToolBar(mToolbar, "选择优惠券", true, true);
        initAdapter(mType, mAskPrize);
    }

    @Override
    public void onStart() {
        super.onStart();

    }


    @Override
    protected void initData() {
        mType = 0;
        pageIndex = 1;
        mLabelsStr = this.getIntent().getStringExtra("ask_lables");
        isShow = this.getIntent().getBooleanExtra("isShow", true);
        mPresenter.getCoupon(mType, pageIndex, pageSize, mLabelsStr);
    }

    @Override
    protected void initEvent() {

    }

    private void initAdapter(int type, double prize) {
        mAdapter = new SelectCouponAdpter(type,prize);
        mEmptyView = new EmptyView(mRvCoupon);
        mRvCoupon.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.bindToRecyclerView(mRvCoupon);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mAdapter.setOnLoadMoreListener(this);
//        mAdapter.setNewData(DataServer.getCouponBoData(20));

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
           if (mAskPrize-mData.get(position).getOffMoney()>0){
               Intent intent = new Intent();
               intent.putExtra("three", mData.get(position));
               setResult(2, intent);
               finish();
           }else {
               showMsg("满"+mData.get(position).getFullMoney()+"元才可以使用！");
           }


    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onLoadMoreRequested() {

        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {
            if (mCurrentCounter < pageSize) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    pageIndex++;
                    mPresenter.getCouponMore(mType, pageIndex, pageSize, mLabelsStr);
                }
            }

        }
    }

    @Override
    public void showCouponSuccess(List<CouponBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        mAdapter.setNewData(workBoPageData);
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showCouponError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
    }

    @Override
    public void showCouponMoreSuccess(List<CouponBo> msg) {
        isErr = false;
        mAdapter.addData(msg);
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showCouponMoreError(String msg) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showCouponEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
